package com.bbyb.operating.examination.service.impl;

import com.bbyb.operating.examination.mapper.UserMapper;
import com.bbyb.operating.examination.model.po.User;
import com.bbyb.operating.examination.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.font.TextHitInfo;
import java.util.List;

/**
 * 账号服务
 * className: UserServiceImpl
 * datetime: 2023/2/10 14:29
 * author: lx
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String addUser(User user) {
        if(userMapper.insert(user) == 1){
            return null;
        }
        return "保存用户信息失败";
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectAll();
    }

    /**
     * 新增
     * @param user
     */
    @Override
    public void insertUser(User user) {
        List<User> users = userMapper.selectAll();
        userMapper.insertUser(user);
        users.forEach(item ->{
            if (item.getUserCode() != null && item.getUserCode().equals(user.getUserCode())) {
                throw new RuntimeException("用户编码不能重复！");
            }
        });
        if (user == null){
            throw new RuntimeException("用户不能为空");
        }

        if (user.getUserCode() == null){
            throw new RuntimeException("用户编号不能为空!");
        }

    }

    /**
     * {
     *   "userCode": "10002",
     *   "userName": "老六",
     *   "phone": "123",
     *   "password": "123",
     *   "email": "123@qq.com",
     *   "createBy": "管理员"
     * }
     */

}
